package LeetCode_Section;

public class CrosspointLinked_II {
    class ListNode {
    int val;
    ListNode next;
    ListNode(int x) {
      val = x;
      next = null;
    }
}

    public class Solution {
        public ListNode detectCycle(ListNode head) {
            ListNode fast = head;
            ListNode slow = head;
            if (head == null) {
                return null;
            }
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                //判断是否有环
                if (fast == slow) {
                    break;
                }
            }
            if (fast == null || fast.next == null) {
                return null;
            }
            fast = head;
            while (fast != slow) {
                fast = fast.next;
                slow = slow.next;
            }
            return slow;
        }
    }
}
